package com.valeandre.euroro;

import java.util.Arrays;

import com.androidplot.xy.BarFormatter;
import com.androidplot.xy.BarRenderer;
import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Menu;

public class DisplayPlotActivity extends Activity {

	public final static String EXTRA_MESSAGE = "com.valeandre.euroro.value";
	public final static String GOLD_EXTRA = "com.valeandre.euroro.goldvalue";
	public final static String SILVER_EXTRA = "com.valeandre.euroro.silvervalue";
	public final static String PLATINUM_EXTRA = "com.valeandre.euroro.platinumvalue";
	public final static String PALLADIUM_EXTRA = "com.valeandre.euroro.palladiumvalue";
	private XYPlot grafico = null;
	private SimpleXYSeries serie=null;
	private String mcurrency;
	private Double mgoldvalue;
	private Double msilvervalue;
	private Double mplatinumvalue;
	private Double mpalladiumvalue;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display_plot);


		Intent intent= getIntent();
		if(intent!=null){
			mcurrency = intent.getStringExtra(EXTRA_MESSAGE);
			mgoldvalue=intent.getDoubleExtra(GOLD_EXTRA, 0);
			msilvervalue=intent.getDoubleExtra(SILVER_EXTRA, 0);
			mplatinumvalue=intent.getDoubleExtra(PLATINUM_EXTRA, 0);
			mpalladiumvalue=intent.getDoubleExtra(PALLADIUM_EXTRA, 0);

			grafico = (XYPlot) findViewById(R.id.XYPlot);

			makeChart(grafico);
		}



	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_display_plot, menu);
		return true;
	}


	public void  makeChart(XYPlot grafico){


		serie = new SimpleXYSeries("values");


		grafico.addSeries(serie, BarRenderer.class, new BarFormatter(Color.YELLOW, Color.RED));
		grafico.setDomainStepValue(4);
		grafico.setTicksPerRangeLabel(3);

		grafico.setDomainValueFormat(new MyFormat());

		Double min=Math.min(mgoldvalue, msilvervalue);
		min=Math.min(min, mplatinumvalue);

		Double max=Math.max(mgoldvalue, msilvervalue);
		max=Math.max(max, mplatinumvalue);
		max=Math.max(max, mpalladiumvalue);

		grafico.setRangeBoundaries(0, max+500, BoundaryMode.FIXED);


		// update our domain and range axis labels:
		grafico.setDomainLabel("Axis");
		Drawable image = getResources().getDrawable(R.drawable.celeste);
		grafico.setBackground(image);
		grafico.getDomainLabelWidget().pack();
		grafico.setRangeLabel(mcurrency);
		grafico.getRangeLabelWidget().pack();
		Number[] series1Numbers = {mgoldvalue, msilvervalue, mplatinumvalue, mpalladiumvalue};
		serie.setModel(Arrays.asList(series1Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);


		BarRenderer barRenderer = (BarRenderer) grafico.getRenderer(BarRenderer.class);
		if(barRenderer != null)
			barRenderer.setBarWidth(30, BarRenderer.BarWidthStyle.FIXED_WIDTH);

		grafico.setGridPadding(30, 0, 30, 0);
		grafico.disableAllMarkup();


	}
}
